package com.hczx.caxa.controller;

import com.hczx.caxa.bean.ResultBean;
import com.hczx.caxa.service.CaxaApiService;
import com.hczx.caxa.util.FTPUtil;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URLEncoder;

@Api(tags = "Caxa相关")
@RestController()
@RequestMapping("/caxa")
public class CaxaApiController {
    private static Logger logger = LoggerFactory.getLogger(CaxaApiController.class);

    final CaxaApiService caxaApi;
    final FTPUtil ftpUtil;

    public CaxaApiController(CaxaApiService caxaApi, FTPUtil ftpUtil) {
        this.caxaApi = caxaApi;
        this.ftpUtil = ftpUtil;
    }

    @GetMapping("/index")
    public String index() {
        logger.debug("caxa-services");
        return "Hello CAXA Services";
    }

    @RequestMapping(value = "/updateCxpFile", method = RequestMethod.POST)
    public ResultBean updateCxpFile(@RequestPart("pdfFile") MultipartFile pdfFile,
                                    @RequestPart("cxpFile") MultipartFile cxpFile) {
        logger.info("pdfFile:{}", pdfFile.getName());
        logger.info("pdfFile:{}", pdfFile.getOriginalFilename());

        logger.info("cxpFile:{}", cxpFile.getName());
        logger.info("cxpFile:{}", cxpFile.getOriginalFilename());

        ResultBean result = ResultBean.buildSuccessResult();
        try {
            String response = caxaApi.updateCxpFile(pdfFile, cxpFile);
            result.setMsg(response);
        } catch (Exception e) {
            e.printStackTrace();
            result = ResultBean.buildFailureResult(e.getLocalizedMessage());
        }
        return result;
    }

    @PostMapping("/getCaxaUrl")
    public String getCaxaUrl(@RequestPart("cxpFile") MultipartFile cxpFile, String number) {
        StringBuffer caxaBuffer = new StringBuffer("");
        String caxaUrl = StringUtils.EMPTY;
        try {

            String cxpFileName = number + ".cxp";
            String xmlFileName = number + ".xml";
            ftpUtil.uploadFile(cxpFile, cxpFileName);

            Resource tempResource = new ClassPathResource("config/caxa.xml");
            ftpUtil.upload(tempResource.getInputStream(), xmlFileName);

            caxaBuffer.append("ftp://").append(ftpUtil.getHost()).append("/");
            caxaBuffer.append("#user").append(ftpUtil.getUsername()).append("#password").append(ftpUtil.getPassword());
            caxaBuffer.append("|N#xmlfile").append(xmlFileName).append("#xmlpath").append("C:\\HS\\CAXA\\").append(xmlFileName);
            caxaBuffer.append("#cxpfile").append(cxpFileName).append("#cxppath").append("C:\\HS\\CAXA\\").append(cxpFileName);

            String caxaEncode = URLEncoder.encode(caxaBuffer.toString(), "UTF-8");
            caxaUrl = "CAXA://" + caxaEncode;

            logger.info("caxaUrl:{}", caxaUrl);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return caxaUrl;
    }

    @PostMapping("/testPost")
    public ResultBean testPost(@RequestPart("file") MultipartFile file) {
        logger.debug("---------->testPost<----------");
        logger.info("file:{}", file.getOriginalFilename());

        ftpUtil.uploadFile(file, file.getOriginalFilename());
        return ResultBean.buildSuccessResult();
    }


}
